﻿using BattleFury.Time;
using Microsoft.Xna.Framework;
using System;

namespace BattleFury.Lib.Time
{
	public class BattleFuryWorldTime
		: WorldTime
	{
		#region Properties
		
		public int MidnightLightValue { get; set; }
		public int SunriseLightValue { get; set; }
		public int NoonLightValue { get; set; }
		public int SunsetLightValue { get; set; }

		public Color MidnightColor { get; set; }
		public Color SunriseColor { get; set; }
		public Color NoonColor { get; set; }
		public Color SunsetColor { get; set; }

		public WorldTimeColor LightColor { get; set; }
		
		public byte LightValue
		{
			get
			{
				float ret = MidnightLightValue;

				if (Hour >= Midnight && Hour < Sunrise)
				{
					var diff = (float)((Sunrise * MinutesInHour) - (Midnight * MinutesInHour));
					var tick = ((Hour * MinutesInHour) + Minute) / diff;
					var lightDiff = (SunriseLightValue - MidnightLightValue);
					if (diff != 0f)
					{
						return Convert.ToByte(MathHelper.Clamp(MidnightLightValue + (tick * lightDiff), MidnightLightValue, SunriseLightValue));
					}
					else
					{
						return Convert.ToByte(MidnightLightValue);
					}
				}

				if (Hour >= Sunrise && Hour < Noon)
				{
					var diff = (float)((Noon * MinutesInHour) - (Sunrise * MinutesInHour));
					var tick = (((Hour * MinutesInHour) + Minute) - (Sunrise * MinutesInHour)) / diff;
					var lightDiff = (NoonLightValue - SunriseLightValue);
					if (diff != 0f)
					{
						return Convert.ToByte(MathHelper.Clamp(SunriseLightValue + (tick * lightDiff), SunriseLightValue, NoonLightValue));
					}
					else
					{
						return Convert.ToByte(SunriseLightValue);
					}
				}

				if (Hour >= Noon && Hour < Sunset)
				{
					var diff = (float)((Sunset * MinutesInHour) - (Noon * MinutesInHour));
					var tick = (((Hour * MinutesInHour) + Minute) - (Noon * MinutesInHour)) / diff;
					var lightDiff = (SunsetLightValue - NoonLightValue);
					if (diff != 0f)
					{
						return Convert.ToByte(MathHelper.Clamp(NoonLightValue + (tick * lightDiff), SunsetLightValue, NoonLightValue));
					}
					else
					{
						return Convert.ToByte(NoonLightValue);
					}
				}

				if (Hour >= Sunset && Hour < (HoursInDay - Sunset) + Sunset)
				{
					var diff = (float)((((HoursInDay - Sunset) + Sunset) * MinutesInHour) - (Sunset * MinutesInHour));
					var tick = (((Hour * MinutesInHour) + Minute) - (Sunset * MinutesInHour)) / diff;
					var lightDiff = (SunsetLightValue - MidnightLightValue);
					if (diff != 0f)
					{
						return Convert.ToByte(MathHelper.Clamp(SunsetLightValue - (tick * lightDiff), MidnightLightValue, SunsetLightValue));
					}
					else
					{
						return Convert.ToByte(SunsetLightValue);
					}
				}

				return Convert.ToByte(ret);
			}
		}

		#endregion

		#region Initialization

		public BattleFuryWorldTime() { }

		public BattleFuryWorldTime(int month, int day, int year, int hour, int minute)
			: base(month, day, year, hour, minute) { }

		protected override void SetDefaultValues()
		{
			Interval = 0f; //45f;
			Sunrise = 6;
			Sunset = 18;

			MinutesInHour = 60;
			HoursInDay = 24;
			DaysInMonth = 30;
			MonthsInYear = 12;

			MidnightLightValue = 25;
			SunriseLightValue = 120;
			SunsetLightValue = 120;
			NoonLightValue = 230;

			MidnightColor = new Color(0f, 0f, 0.3f, 1f);
			SunriseColor = new Color(0.9f, 0.6f, 1f, 1f);
			SunsetColor = new Color(0.9f, 0.6f, 1f, 1f);
			NoonColor = new Color(1f, 1f, 1f, 1f);

			MilitaryTime = true;
			LightColor = new WorldTimeColor(this);

			IsEnabled = true;
		}

		#endregion

		#region Methods
		
		public override void Update(GameTime gameTime)
		{
			LightColor.WorldTime = this;

			base.Update(gameTime);
		}

		#endregion
	}
}